package com.zxy.ziems.server.weixin;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WeChatService {


    private final WxCpService wxCpService;

    @Autowired
    public WeChatService(WxCpConfigStorage configStorage) {
        wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(configStorage);
    }


    public String getAccessToken() throws WxErrorException {
        return wxCpService.getAccessToken();
    }

    public void sendMessageToUser(String mobile, String content) throws WxErrorException {

       String userId =  wxCpService.getUserService().getUserId(mobile);

        WxCpMessage message = WxCpMessage.TEXT().toUser(userId).content(content).build();
        wxCpService.getMessageService().send(message);


    }
}
